import Vue from "vue";
import Router from "vue-router";
import RouterView from "@/view/RouterView/RouterView.vue";
import Setting from "@/view/setting/setting.vue";
import TradeHis from "@/view/tradeHis/tradeHis.vue";
import Bill from "@/view/bill/bill.vue";
import Authorize from "@/view/authorize/authorize.vue";
import Wallet from "@/view/wallet/wallet.vue";
import TakeCash from "@/view/takeCash/takeCash.vue";
import NewsList from "@/view/newsList/newsList.vue";
import News from "@/view/news/news.vue";
import Home from "@/view/home/home.vue";
import Trade from "@/view/trade/trade.vue";
import Log from "@/view/log/log.vue";
import Mine from "@/view/mine/mine.vue";
import SysSetting from "@/view/sysSetting/sysSetting.vue";
import TransactionHis from "@/view/transactionHis/transactionHis.vue";
import Information from "@/view/information/information.vue";
import Personal from "@/view/personal/personal.vue";
import Phone from "@/view/phone/phone.vue";
import Password from "@/view/password/password.vue";
import PayPSD from "@/view/payPSD/payPSD.vue";
import Recharge from "@/view/recharge/recharge.vue";
import Conversion from "@/view/conversion/conversion.vue";
import Login from "@/view/login/login.vue";
import Register from "@/view/register/register.vue";
import Community from "@/view/community/community.vue";
import Share from "@/view/share/share.vue";

Vue.use(Router);

Router.prototype.goBack = function() {
    this.isBack = true;
    window.history.back();
    // window.history.go(-1)
};

Router.prototype.push = function push(location, onComplete, onAbort) {
    var this$1 = this;
    this$1.isBack = false;

    // $flow-disable-line
    if (!onComplete && !onAbort && typeof Promise !== "undefined") {
        return new Promise(function(resolve, reject) {
            this$1.history.push(location, resolve, reject);
        });
    } else {
        this.history.push(location, onComplete, onAbort);
    }
};

const originalPush = Router.prototype.push;
//修改原型对象中的push方法
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};

const router = new Router({
    mode: "hash",
    routes: [{
            path: "/login",
            name: "login",
            component: Login,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: "/register",
            name: "register",
            component: Register,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: "/",
            name: "RouterView",
            component: RouterView,
            redirect: "/home",
            meta: {
                keepAlive: true,
            },

            children: [{
                    path: "home",
                    name: "Home",
                    component: Home,
                    meta: {
                        keepAlive: true,
                        active: 0,
                    },
                },
                {
                    path: "trade",
                    name: "trade",
                    component: Trade,
                    meta: {
                        keepAlive: true,
                        active: 1,
                    },
                },
                {
                    path: "log",
                    name: "log",
                    component: Log,
                    meta: {
                        keepAlive: true,
                        active: 2,
                    },
                },
                {
                    path: "information",
                    name: "Information",
                    component: Information,
                    meta: {
                        keepAlive: true,
                        active: 3,
                    },
                },
                {
                    path: "mine",
                    name: "mine",
                    component: Mine,
                    meta: {
                        keepAlive: true,
                        active: 4,
                    },
                },
            ],
        },
        {
            path: "/tradeHis",
            name: "TradeHis",
            component: TradeHis,
            meta: {
                keepAlive: true,
            },
        },
        {
            path: "/bill",
            name: "Bill",
            component: Bill,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: "/setting",
            name: "Setting",
            component: Setting,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: "/authorize",
            name: "Authorize",
            component: Authorize,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: "/wallet",
            name: "Wallet",
            component: Wallet,
            meta: {
                keepAlive: false,   // 不缓存
            },
        },
        {
            path: "/takeCash",
            name: "TakeCash",
            component: TakeCash,
            meta: {
                keepAlive: true,
            },
        },
        {
            path: "/newsList",
            name: "NewsList",
            component: NewsList,
            meta: {
                keepAlive: true,
            },
        },
        {
            path: "/news",
            name: "News",
            component: News,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: "/sysSetting",
            name: "SysSetting",
            component: SysSetting,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: "/transactionHis",
            name: "TransactionHis",
            component: TransactionHis,
            meta: {
                keepAlive: true,
            },
        },
        {
            path: "/personal",
            name: "personal",
            component: Personal,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: "/phone",
            name: "phone",
            component: Phone,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: "/password",
            name: "password",
            component: Password,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: "/payPSD",
            name: "payPSD",
            component: PayPSD,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: "/recharge",
            name: "recharge",
            component: Recharge,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: "/conversion",
            name: "conversion",
            component: Conversion,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: "/community",
            name: "community",
            component: Community,
            meta: {
                keepAlive: false,
            },
        },
        {
            path: "/share",
            name: "share",
            component: Share,
            meta: {
                keepAlive: false,
            },
        },
    ],
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition;
        } else {
            return {
                x: 0,
                y: 0,
            };
        }
    },
});

// router.beforeEach((to, from, next) => {
//   //判断是否需要缓存

// })

export default router;

// RouterModel.beforeEach((to, from, next) => {
//   // const { user_id } = getLocalStorage('user_id');
//   // if (!user_id) {
//   //   if (to.meta.login) {
//   //     next({ name: 'login', query: { redirect: to.name } });
//   //     return;
//   //   }
//   // }
//   const pageTitle = to.meta.title ? to.meta.title : '未来鑫城';
//   window.document.title = pageTitle;
//   next();
// });

// export default RouterModel;